﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Billing.Dtos;
using Billing.Windows;
using Billing.Utilities;
using System.Data;

namespace Billing.Controls
{
    public partial class VendorBillingGridItemControl : UserControl
    {
        public int VendorID { get; set; }

        public VendorBillingGridItemControl(int companyID)
        {
            InitializeComponent();
            VendorID = companyID;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            txtCode.Focus();
        }

        private void txtCode_GotFocus(object sender, RoutedEventArgs e)
        {
            txtCode.Background = Brushes.White;
            txtCode.Foreground = Brushes.Black;
        }

        private void txtCode_LostFocus(object sender, RoutedEventArgs e)
        {
            txtCode.Background = Brushes.Transparent;
            txtCode.Foreground = Brushes.White;
        }

        private void txtQty_GotFocus(object sender, RoutedEventArgs e)
        {
            txtQty.Background = Brushes.White;
            txtQty.Foreground = Brushes.Black;
            txtQty.SelectAll();
        }

        private void txtQty_LostFocus(object sender, RoutedEventArgs e)
        {
            txtQty.Background = Brushes.Transparent;
            txtQty.Foreground = Brushes.White;
        }

        private void txtCode_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                if (txtCode.Text != string.Empty)
                {
                    DataTable dt = Database.GetItemDetailsForVendor(VendorID, txtCode.Text.ToString());

                    if (dt == null)
                    {
                        MessageBox.Show("No Item Exists", "Invalid Item", MessageBoxButton.OK, MessageBoxImage.Error);
                        //txtCode.Clear();
                        txtDesc.Clear();
                        txtPrice.Clear();
                        txtPrice.Tag = null;
                        grp.Text = string.Empty;
                        acceptNegativeStock.Text = string.Empty;
                        return;
                    }

                    txtDesc.Text = dt.Rows[0][0].ToString();
                    double price = double.Parse(dt.Rows[0][1].ToString());
                    txtPrice.Tag = (price * double.Parse(txtQty.Text)).ToString();
                    txtPrice.Text = (price * double.Parse(txtQty.Text)).ToString();
                    grp.Text = dt.Rows[0][2].ToString();
                    acceptNegativeStock.Text = dt.Rows[0][3].ToString();
                }
            }
            catch (Exception) { }
        }

        private void txtCode_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter && txtCode.Text != string.Empty && txtDesc.Text != string.Empty)
            {
                txtQty.Focus();
            }
            else if (e.Key == Key.Delete)
            {
                txtCode.Clear();
                txtDesc.Clear();
                txtQty.Text = "1";
                txtPrice.Clear();
                txtPrice.Tag = null;
            }
        }

        private void txtQty_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                double value;
                if (!(double.TryParse(txtQty.Text, out value)))
                {
                    if (txtQty.Text != string.Empty)
                    {
                        MessageBox.Show("Invalid Quantity Field", "Invalid Qty", MessageBoxButton.OK, MessageBoxImage.Error);
                        txtQty.Clear();
                    }
                    return;
                }
                else if (txtPrice != null)
                {
                    if (txtPrice.Tag != null)
                        txtPrice.Text = (value * double.Parse(txtPrice.Tag.ToString())).ToString();
                }
            }
            catch (Exception) { }
        }
    }
}
